<?php
/**
 * CLogStream class file.
 * 用文件存储日志.
 * 需要将文件地址做为参数赋给构造方法.
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CLogStream.php 1 2010-08-10 14:40:00Z huan.song $
 * @package system.log
 * @since 2.0
 */
class CLogStream extends CLog
{
	/**
     * @var null|stream 文件指针.
     */
    protected $_stream = null;
	
    public function __construct($path)
    {
    	if (!$this->_stream = @fopen($path, 'a', false))
    		throw new CLogException('CLogStream:__construct path cannot be opened with mode "a"', array('{path}' => $path));
    } 

    /**
     * 关闭文件指针.
     * @return void.
     */
    public function shutdown()
    {
        if (is_resource($this->_stream)) 
            fclose($this->_stream);
    }

    /**
     * 向文件中写一条日志.
     * @param  array  $event  event data.
     * @return void.
     */
    protected function _write($event)
    {
        $line = $this->_format($event);
		
        if (false === @fwrite($this->_stream, $line)) 
            throw new CLogException('CLogStream:_write Unable to write to stream');
    }
}
